// area.h : Contains the definition for the area class, which holds
// an area with the objects inside it

#pragma once
#include "types.h"

class CObjectManager;
class CAreaView;

//Area class
class CArea
{
public:
	CArea(CObjectManager*, gmMachine*, CAreaView*);
	~CArea(void);

	//Adds a new object to the room
	void AddObject( const std::string& file, const ParameterList& );

	//Renders the area
	void Render( void );

	//Registers a transport operator
	void RegisterTransport( ObjectID, CArea* pDest, const bug::math::CVector& newPos );

	//Removes ownership of an object (not safe, use only by OM)
	void Disown( ObjectID );

	//Takes ownership of an object (not safe, use only by OM)
	void Own( ObjectID );

	//Updates the area
	void Update( float fElapsed, float fTotal );
	
	//Comparison function to sort the objects by depth
	static bool CompareDepth( ObjectID, ObjectID, CObjectManager* );

	//Sets the background name
	void SetBackground( const std::string& name );

	//Gets the user object
	gmUserObject* GetUser( void ) { return m_pUser; }

	//Gets an object by name
	ObjectID GetByName( const std::string& name );

	//Gets the active player ID
	unsigned int GetActivePlayerID( void );

	//Sets this room as an active player area 
	void SetPlayerArea( unsigned int PlayerID );

	//Object list type
	typedef std::list<ObjectID> ObjectList;

private:

	//Object list
	ObjectList m_objects;

public:

	//Begin object iterator
	ObjectList::iterator StartIterate( void ) { return m_objects.begin(); }

	//End object iterator
	ObjectList::iterator EndIterator( void ) { return m_objects.end(); }

private:

	//Object manager
	CObjectManager* m_pOManager;

	//Background image name
	std::string m_backgroundName;

	//Background texture
	bug::render::TexturePointer m_background;

	//User object
	gmUserObject* m_pUser;

	//Stored GM machine
	gmMachine* m_pGM;

	//Parent
	CAreaView* m_pOwner;

	//Structure that contains information of an object transport
	struct TransportInfo
	{
		ObjectID toMove; //Object to move
		bug::math::CVector pos; //Position to move after change
		CArea* pDest; //Destination area
	};

	//List of transports
	typedef std::list<TransportInfo> TransportList;
	TransportList m_transports;
};

//Loads an area from a file
bool LoadFromFile( const std::string& filename, CArea* pArea );